package com.jindi.jacococli.api.entrance.rest;

import com.jindi.jacococli.biz.business.action.BatchReportServiceAction;
import com.jindi.jacococli.biz.business.model.ReportServiceProgress;
import com.jindi.jacococli.biz.business.model.ServiceProject;
import com.jindi.jacococli.biz.business.model.ServiceProjectList;
import com.jindi.jacococli.biz.business.task.ReportTaskManager;
import com.jindi.jacococli.biz.config.ServiceProjectConfig;
import com.jindi.jacococli.biz.constant.Constant;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@RestController
public class JacocoReportController {
    @Resource
    private BatchReportServiceAction batchReportServiceAction;

    @Resource
    private ServiceProjectConfig serviceProjectConfig;

    @PostMapping(value = "/report")
    public String batchCreateReport(@RequestBody ServiceProjectList serviceProjectList) {
        List<ServiceProject> projects = serviceProjectList.getServices();
        if (CollectionUtils.isEmpty(projects)) {
            return "services 请指定分析的报告";
        }
        String email = serviceProjectList.getEmail();
        if (StringUtils.isBlank(email) || !email.endsWith(Constant.EMAIL_SUFFIX)) {
            return "email 请填写天眼查的邮件地址";
        }
        for (ServiceProject project : projects) {
            String serviceName = project.getServiceName();
            if (StringUtils.isBlank(serviceName)) {
                return "serviceName is blank, 必须不为空";
            }
            String projectName = project.getProjectName();
            if (StringUtils.isBlank(projectName) || !projectName.contains(Constant.BACKSLASH)
                    || projectName.split(Constant.BACKSLASH).length != Constant.INT2) {
                return "projectName is blank, 必须是项目组+项目名, 以'/'分割 ";
            }
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        serviceProjectList.setUuid(uuid);
        batchReportServiceAction.doAction(serviceProjectList);
        return "您可以使用该路径【GET:/report/status/" + uuid + "】查询本次分析的进度";
    }

    @PostMapping(value = "/report/config")
    public String batchCreateReportConfig(@RequestBody ServiceProjectList serviceProjectList) {

        List<ServiceProject> serviceProjects = serviceProjectConfig.getProjects();
        if (CollectionUtils.isEmpty(serviceProjects)) {
            return "当前项目没有配置默认的项目集合,请先配置默认的项目集合";
        }
        serviceProjectList.setServices(serviceProjects);
        String email = serviceProjectList.getEmail();
        if (StringUtils.isBlank(email) || !email.endsWith(Constant.EMAIL_SUFFIX)) {
            return "email 请填写天眼查的邮件地址";
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        serviceProjectList.setUuid(uuid);
        batchReportServiceAction.doAction(serviceProjectList);
        return "您可以使用该路径【GET:/report/status/" + uuid + "】查询本次分析的进度";
    }

    @GetMapping(value = "/report/status/{uuid}")
    public String batchCreateReportConfig(@PathVariable(name = "uuid") String uuid) {
        ReportServiceProgress pro = ReportTaskManager.get(uuid);
        if (Objects.isNull(pro)) {
            return "不存在您传入的任务标识;已经生成完成发送您的天眼查邮箱或者传入的不正确";
        }
        return String.format(Constant.DESCRIBE_CONTENT, pro.getUuid(), pro.getDescription(), pro.getTotal(), pro.getOver());
    }

}
